package org.example.contoller;

import lombok.RequiredArgsConstructor;
import org.deeplearning4j.nn.graph.ComputationGraph;
import org.example.processor.FeatureExtractor;
import org.example.processor.ImageProcessor;
import org.example.util.SimilaritySearch;
import org.nd4j.linalg.api.ndarray.INDArray;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;


@RestController
@RequestMapping("/api/goods")
@RequiredArgsConstructor
public class ChatController {
    private final ComputationGraph model;


    // 步骤5：更新控制器
    // 在原有chat方法中增加订单处理分支
    @PostMapping("/sames")
    public ResponseEntity<String> sames(MultipartFile file) throws IOException {
        // 3. 处理用户查询图片
        String uploadDir = System.getProperty("java.io.tmpdir"); // 示例用临时目录
        String realPath = uploadDir + File.separator + file.getOriginalFilename();
        file.transferTo(new File(realPath)); // 保存文件
        INDArray queryImage = ImageProcessor.preprocessImage(realPath);
        INDArray queryFeature = ImageProcessor.extractFeatures(queryImage, model);

        // 4. 搜索相似商品
        String similarProductId = SimilaritySearch.findMostSimilar(queryFeature);
        System.out.println("最相似的商品ID: " + similarProductId);
        return ResponseEntity.ok("最相似的商品ID: " + similarProductId);
    }
}
